package conf

/*******************************************************************************
Copyright:cloud
Author:cloudapex@126.com
Version:1.0
Date:2020-06-12
Description: 主配置文件
*******************************************************************************/
import (
	"encoding/json"
	"fmt"
	"io/ioutil"

	"sync"

	"gitee.com/cloudapex/ulern/cto"
	"gitee.com/cloudapex/ulern/util"
)

type CommonSetting struct {
	ServerId   string `json:"serverId"`
	ListenAddr string `json:"listenAddr"`
}
type GServerOption struct {
	RunMode string `json:"runMode"`

	Tls  GSerOptTLS  `json:"tls"`
	Cred GSerOptCred `json:"cred"`
}
type GSerOptTLS struct {
	Enable  bool   `json:"enable"`
	CrtFile string `json:"crtFile"`
	KeyFile string `json:"keyFile"`
}
type GSerOptCred struct {
	Enable     bool   `json:"enable"`
	AuthScheme string `json:"authScheme"` // service
}
type ReportSetting struct {
	Enable bool   `json:"enable"`
	Env    string `json:"env"`
	ToAddr string `json:"toAddr"`
}

//------------------------------------------------------------------------------
//=====================================Main=====================================
//------------------------------------------------------------------------------
var Main *mainConf

func init() { Main = cto.Preload("MT_Main", &mainConf{}).(*mainConf) }

type mainConf struct {
	mutx sync.Mutex
	main *mainMain

	path string // 自定义主配置文件路径
}
type mainMain struct {
	Common   CommonSetting `json:"common"`
	GSerOpts GServerOption `json:"gsOpts"`
	Report   ReportSetting `json:"report"`
}

func (this *mainConf) Path(path string) { this.path = path }

func (this *mainConf) HandleInit() error {
	this.main = &mainMain{}
	data, err := ioutil.ReadFile(this.HandlePath())
	if err != nil {
		return err
	}
	return json.Unmarshal(data, this.main)
}
func (this *mainConf) HandlePath() string {
	return util.ExePathJoin(fmt.Sprintf("confs/%s.json", this.path))
}

func (this *mainConf) HandleContent() string {
	b, err := json.MarshalIndent(this.main, "", "\t")
	if err != nil {
		return err.Error()
	}
	return string(b)
}
func (this *mainConf) HandleReload() error {
	data, err := ioutil.ReadFile(this.HandlePath())
	if err != nil {
		return err
	}

	temp := &mainMain{Common: CommonSetting{}}
	if err := json.Unmarshal(data, temp); err != nil {
		return err
	}
	defer util.UnLock(util.Lock(&this.mutx))
	// update some fields
	return cto.ErrSettingNotNeed
}

//------------------------------------------------------------------------------
//================================开放方法=======================================
//------------------------------------------------------------------------------
func (this *mainConf) Stg() *mainMain {
	defer util.UnLock(util.Lock(&this.mutx))
	return this.main
}
func (this *mainConf) ServerId() string   { return this.Stg().Common.ServerId }
func (this *mainConf) ListenAddr() string { return this.Stg().Common.ListenAddr }

// GSerOptions
func (this *mainConf) GSerOption() GServerOption { return this.Stg().GSerOpts }
func (this *mainConf) GSerOptTLS() GSerOptTLS    { return this.Stg().GSerOpts.Tls }
func (this *mainConf) GSerOptCred() GSerOptCred  { return this.Stg().GSerOpts.Cred }

// ReportSetting
func (this *mainConf) Report() ReportSetting { return this.Stg().Report }
